#include "MainScene.h"
#include "GameConfig.h"
#include "LevelConfig.h"
#include "ccTypes.h"
#include "GlobalData.h"
#include "ObjectDataDefine.h"
#include "TestGameScene.h"
#include "PlaneFactory.h"
#include "PlayerPlane.h"
#include "PlaneBank.h"
#include "MathUtil.h"
#include "BulletsBank.h"
#include "WeaponBank.h"
#include "PetsBank.h"
#include "EnemyBank.h"
#include "StrategyBank.h"
#include "ItemBank.h"
#include "ItemProcessorBank.h"
#include "EnemyActionFactory.h"
#include "Config.h"




MainScene::MainScene(void)
{
	_lblGold = NULL;
	_lblUserLevel = NULL;
	_lblPlaneId = NULL;
	_lblPlaneType = NULL;
	_lblPlaneLevel = NULL;
	_lblPlaneDamage = NULL;
	_lblPlaneHealth = NULL;
	_lblPlaneSpeed = NULL;
	_lblPlaneWeapon = NULL;
	_lblPetType = NULL;
	_lblPetWeapon = NULL;

	_planeIdIndex = 0;
}


MainScene::~MainScene(void)
{
	CC_SAFE_RELEASE(_lblGold);
	CC_SAFE_RELEASE(_lblUserLevel);
	CC_SAFE_RELEASE(_lblPlaneId);
	CC_SAFE_RELEASE(_lblPlaneType);
	CC_SAFE_RELEASE(_lblPlaneLevel);
	CC_SAFE_RELEASE(_lblPlaneDamage);
	CC_SAFE_RELEASE(_lblPlaneHealth);
	CC_SAFE_RELEASE(_lblPlaneSpeed);
	CC_SAFE_RELEASE(_lblPlaneWeapon);
	CC_SAFE_RELEASE(_lblPetType);
	CC_SAFE_RELEASE(_lblPetWeapon);
}

bool MainScene::init()
{
	if (!CCLayer::init())
		return false;

	setKeypadEnabled(true);
	

	CCMenu* menu = CCMenu::create();

	
	// Load Game Config
	GameConfig::sharedGameConfig()->init();
	LevelConfig::sharedLevelConfig()->loadConfig();
	PlaneBank::sharedPlaneBank()->initBank();
	BulletsBank::sharedBulletBank()->initBank();
	WeaponBank::sharedWeaponBank()->initBank();
	PetBank::sharedPetBank()->initBank();
	EnemyBank::sharedEnemyBank()->initBank();
	StrategyBank::sharedStrategyBank()->initBank();
	ItemBank::sharedItemBank()->initBank();
	ItemProcessorBank::sharedItemProcessorBank()->initBank();
	EnemyActionFactory::sharedActionFactory()->initFactory();
    
    
    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("Common.plist");
	

	CCSprite* goldIcon = CCSprite::createWithSpriteFrameName("gold_icon.png");
	goldIcon->setPosition(ccp(58, 1020));
	goldIcon->setScale(0.8f);
	addChild(goldIcon);

// 	CCLabelAtlas* lblGold = CCLabelAtlas::create("100", "labelatlasimg.png", 24, 32, 0);
// 	lblGold->setAnchorPoint(CCPointZero);
// 	lblGold->setPosition(ccp(100, 700));
// 	lblGold->setString("1212");
// 	lblGold->setScale(1);
// 	addChild(lblGold);

	// GOLD
	CCLabelTTF* lblGold2 = CCLabelTTF::create("0", "arial", 70, CCSizeMake(200, 80), kCCTextAlignmentLeft, kCCVerticalTextAlignmentCenter);
	lblGold2->setAnchorPoint(CCPointZero);
	lblGold2->setPosition(ccp(108, 1003));
	addChild(lblGold2);
	setLableGold(lblGold2);


	// USER LEVEL
	CCLabelTTF* lblUserLevel = CCLabelTTF::create("0", "arial", 70, CCSizeMake(200, 80), kCCTextAlignmentLeft, kCCVerticalTextAlignmentCenter);
	lblUserLevel->setAnchorPoint(CCPointZero);
	lblUserLevel->setPosition(ccp(360, 903));
	addChild(lblUserLevel);
	setUserLevel(lblUserLevel);

	// PLANE ID
	CCLabelTTF* lblPlaneId = CCLabelTTF::create("0", "arial", 30, CCSizeMake(200, 70), kCCTextAlignmentLeft, kCCVerticalTextAlignmentCenter);
	lblPlaneId->setAnchorPoint(CCPointZero);
	lblPlaneId->setPosition(ccp(360, lblUserLevel->getPositionY() - 120));
	addChild(lblPlaneId);
	setPlaneId(lblPlaneId);

	CCMenuItemImage* upButtonPlane = CCMenuItemImage::create("upButton.png", "upButton.png", "upButton.png", this, SEL_MenuHandler(&MainScene::callbackPlaneIdUp));
	upButtonPlane->setPosition(ccp(360 + 200, lblPlaneId->getPositionY() + 25));
	menu->addChild(upButtonPlane);

	CCMenuItemImage* downButtonPlane = CCMenuItemImage::create("upButton.png", "upButton.png", "upButton.png", this, SEL_MenuHandler(&MainScene::callbackPlaneIdDown));
	downButtonPlane->setPosition(ccp(360 -100, lblPlaneId->getPositionY() + 25));
	downButtonPlane->setScaleY(-1);
	menu->addChild(downButtonPlane);

	// PLANE TYPE
	CCLabelTTF* lblPlaneType = CCLabelTTF::create("0", "arial", 30, CCSizeMake(200, 70), kCCTextAlignmentLeft, kCCVerticalTextAlignmentCenter);
	lblPlaneType->setAnchorPoint(CCPointZero);
	lblPlaneType->setPosition(ccp(360, lblPlaneId->getPositionY() - 120));
	addChild(lblPlaneType);
	setPlaneType(lblPlaneType);

	// PLANE LEVEL
	CCLabelTTF* lblPlaneLevel = CCLabelTTF::create("0", "arial", 30, CCSizeMake(200, 70), kCCTextAlignmentLeft, kCCVerticalTextAlignmentCenter);
	lblPlaneLevel->setAnchorPoint(CCPointZero);
	lblPlaneLevel->setPosition(ccp(360, lblPlaneType->getPositionY() - 120));
	addChild(lblPlaneLevel);
	setPlaneLevel(lblPlaneLevel);

	// PLANE DAMAGE
	CCLabelTTF* lblPlanedamage = CCLabelTTF::create("00", "arial", 30, CCSizeMake(200, 70), kCCTextAlignmentLeft, kCCVerticalTextAlignmentCenter);
	lblPlanedamage->setAnchorPoint(CCPointZero);
	lblPlanedamage->setPosition(ccp(50, lblPlaneType->getPositionY() - 120));
	addChild(lblPlanedamage);
	setPlaneDamage(lblPlanedamage);
	// PLANE HEALTH
	CCLabelTTF* lblPlaneHealth = CCLabelTTF::create("00", "arial", 30, CCSizeMake(200, 70), kCCTextAlignmentLeft, kCCVerticalTextAlignmentCenter);
	lblPlaneHealth->setAnchorPoint(CCPointZero);
	lblPlaneHealth->setPosition(ccp(50, lblPlaneType->getPositionY() - 120 - 50));
	addChild(lblPlaneHealth);
	setPlaneHealth(lblPlaneHealth);
	// PLANE SPEED
	CCLabelTTF* lblPlaneSpeed = CCLabelTTF::create("00", "arial", 30, CCSizeMake(200, 70), kCCTextAlignmentLeft, kCCVerticalTextAlignmentCenter);
	lblPlaneSpeed->setAnchorPoint(CCPointZero);
	lblPlaneSpeed->setPosition(ccp(50, lblPlaneType->getPositionY() - 120 - 100));
	addChild(lblPlaneSpeed);
	setPlaneSpeed(lblPlaneSpeed);

	// PLANE WEAPONE
	CCLabelTTF* lblPlaneWeapon = CCLabelTTF::create("00", "arial", 30, CCSizeMake(200, 70), kCCTextAlignmentLeft, kCCVerticalTextAlignmentCenter);
	lblPlaneWeapon->setAnchorPoint(CCPointZero);
	lblPlaneWeapon->setPosition(ccp(50, lblPlaneType->getPositionY() - 120 - 150));
	addChild(lblPlaneWeapon);
	setPlaneWeapon(lblPlaneWeapon);

	CCMenuItemImage* upButtonWeapon = CCMenuItemImage::create("upButton.png", "upButton.png", "upButton.png", this, SEL_MenuHandler(&MainScene::callbackPlayerWeaponUp));
	upButtonWeapon->setPosition(ccp(360 + 150, lblPlaneType->getPositionY() - 100));
	menu->addChild(upButtonWeapon);

	CCMenuItemImage* downButtonWeapon = CCMenuItemImage::create("upButton.png", "upButton.png", "upButton.png", this, SEL_MenuHandler(&MainScene::callbackPlayerWeaponDown));
	downButtonWeapon->setPosition(ccp(360 -100, lblPlaneType->getPositionY() - 100));
	downButtonWeapon->setScaleY(-1);
	menu->addChild(downButtonWeapon);



	// PET ID
	CCLabelTTF* lblPetId = CCLabelTTF::create("0", "arial", 30, CCSizeMake(200, 70), kCCTextAlignmentLeft, kCCVerticalTextAlignmentCenter);
	lblPetId->setAnchorPoint(CCPointZero);
	lblPetId->setPosition(ccp(360, lblPlaneWeapon->getPositionY() - 120));
	addChild(lblPetId);
	setPetType(lblPetId);

	CCMenuItemImage* upButtonPetType = CCMenuItemImage::create("upButton.png", "upButton.png", "upButton.png", this, SEL_MenuHandler(&MainScene::callbackPetIdUp));
	upButtonPetType->setPosition(ccp(360 + 250, lblPetId->getPositionY() + 25));
	menu->addChild(upButtonPetType);

	CCMenuItemImage* downButtonPetType = CCMenuItemImage::create("upButton.png", "upButton.png", "upButton.png", this, SEL_MenuHandler(&MainScene::callbackPetIdDown));
	downButtonPetType->setPosition(ccp(360 -100, lblPetId->getPositionY() + 25));
	downButtonPetType->setScaleY(-1);
	menu->addChild(downButtonPetType);

	// PET Weapon
	CCLabelTTF* lblPetWeapon = CCLabelTTF::create("0", "arial", 30, CCSizeMake(200, 70), kCCTextAlignmentLeft, kCCVerticalTextAlignmentCenter);
	lblPetWeapon->setAnchorPoint(CCPointZero);
	lblPetWeapon->setPosition(ccp(360, lblPetId->getPositionY() - 120));
	addChild(lblPetWeapon);
	setPetWeapon(lblPetWeapon);


	CCMenuItemFont* _start = CCMenuItemFont::create("START", this, SEL_MenuHandler(&MainScene::callBackStartClick));
	_start->setFontSize(100);
	_start->setAnchorPoint(CCPointZero);
	_start->setPosition(ccp(400, 150));
	menu->addChild(_start);

	CCMenuItemImage* _login = CCMenuItemImage::create("facebook_img.png", "facebook_img.png", "facebook_img.png", this, SEL_MenuHandler(&MainScene::callbackFBLoginClick));
	_login->setPosition(ccp(RESOLUTION_WIDTH - 138, 10));
	_login->setAnchorPoint(CCPointZero);
	menu->addChild(_login);

	menu->setPosition(CCPointZero);
	menu->setAnchorPoint(CCPointZero);
	addChild(menu);

	// Set du lieu gia
	PlayerData* playerData;
	_planeIdIndex = 0;
	_petIdIndex = 0;
	if (GlobalData::sharedData()->getPlayerData() == NULL)
	{
		const char* id = PlaneBank::sharedPlaneBank()->getIdByIndex(_planeIdIndex)->getCString();
		playerData = new PlayerData();
		playerData->autorelease();
		playerData->setGold(1000);
		playerData->setUserExp(200);
		playerData->setPlaneId(ccs(id));
		playerData->setPlaneLevel(1);
		playerData->setPetType(PetBank::sharedPetBank()->getPetIdByIndex(_petIdIndex));
		playerData->setPetWeapon(1);

		GlobalData::sharedData()->setPlayerData(playerData);
	}
	
	playerData = GlobalData::sharedData()->getPlayerData();

	PlaneData* data = dynamic_cast<PlaneData*>(PlaneBank::sharedPlaneBank()->getPlaneFactoryById((char*)playerData->getPlaneId()->getCString())->getLevels()->objectAtIndex(playerData->getPlaneLevel() - 1));

	

	updatePlaneInfo();



	return true;
}

void MainScene::callBackStartClick( CCObject* sender )
{
	CCDirector::sharedDirector()->replaceScene(TestGameScene::create());
}

void MainScene::callbackPlayerWeaponUp( CCObject* sender )
{
	PlayerData* playerData = GlobalData::sharedData()->getPlayerData();
	PlaneFactory* pF = PlaneBank::sharedPlaneBank()->getPlaneFactoryById((char*)playerData->getPlaneId()->getCString());
	CCArray* list = pF->getLevels();
	int wp = GlobalData::sharedData()->getPlayerData()->getPlaneLevel();

	// Kiem tra va tru tien
	if (wp >= list->count())
	{
		CCMessageBox("Max level", "Info");
		return;
	}
	int price = dynamic_cast<PlaneData*>(list->objectAtIndex(wp))->getPrice();
	if (playerData->getGold() >= price)
	{
		playerData->setGold(playerData->getGold() - price);
	} else
	{
		CCMessageBox("Eo du tien", "Info");
		return;
	}
		//
	//wp --;
	if (wp < 0)
		wp = 0;
	if (wp >= (int)list->count())
		wp = (int)list->count() - 1;
	wp++;

	GlobalData::sharedData()->getPlayerData()->setPlaneLevel(wp);
	_lblPlaneLevel->setString(MathUtil::IntToString(GlobalData::sharedData()->getPlayerData()->getPlaneLevel()).c_str());

	updatePlaneInfo();
}

void MainScene::callbackPlayerWeaponDown( CCObject* sender )
{
	PlayerData* playerData = GlobalData::sharedData()->getPlayerData();
	PlaneFactory* pF = PlaneBank::sharedPlaneBank()->getPlaneFactoryById((char*)playerData->getPlaneId()->getCString());
	CCArray* list = pF->getLevels();
	int wp = GlobalData::sharedData()->getPlayerData()->getPlaneLevel() - 1;
	wp--;
	if (wp < 0)
		wp = 0;
	if (wp >= (int)list->count())
		wp = (int)list->count() - 1;
	wp++;
	GlobalData::sharedData()->getPlayerData()->setPlaneLevel(wp);
	_lblPlaneLevel->setString(MathUtil::IntToString(GlobalData::sharedData()->getPlayerData()->getPlaneLevel()).c_str());

	updatePlaneInfo();
}

void MainScene::onEnter()
{
	CCLayer::onEnter();
	// Load config
	
}

void MainScene::onExit()
{
	CCLayer::onExit();
}

CCScene* MainScene::scene()
{
	CCScene* scene = CCScene::create();
	scene->addChild(MainScene::create());
	return scene;
}

void MainScene::keyBackClicked()
{
	BulletsBank::sharedBulletBank()->clearBank();
	WeaponBank::sharedWeaponBank()->clearBank();
	PlaneBank::sharedPlaneBank()->clearBank();
	PetBank::sharedPetBank()->clearBank();
	EnemyBank::sharedEnemyBank()->clearBank();
	StrategyBank::sharedStrategyBank()->clearBank();
	ItemBank::sharedItemBank()->clearBank();
	ItemProcessorBank::sharedItemProcessorBank()->clearBank();
	EnemyActionFactory::sharedActionFactory()->clearFactory();
	LevelConfig::sharedLevelConfig()->clearConfig();
	GlobalData::sharedData()->clearData();
	GameConfig::sharedGameConfig()->clearGameConfig();
	CCDirector::sharedDirector()->end();
}

void MainScene::updatePlaneInfo()
{
	PlayerData* playerData = GlobalData::sharedData()->getPlayerData();

	PlaneData* data = dynamic_cast<PlaneData*>(PlaneBank::sharedPlaneBank()->getPlaneFactoryById((char*)playerData->getPlaneId()->getCString())->getLevels()->objectAtIndex(playerData->getPlaneLevel() - 1));
	_lblPlaneDamage->setString(MathUtil::IntToString(data->getDamage()).c_str());
	_lblPlaneHealth->setString(MathUtil::IntToString(data->getHealth()).c_str());
	_lblPlaneSpeed->setString(MathUtil::IntToString(data->getSpeed()).c_str());

	_lblPlaneWeapon->setString(data->getWeapon()->getCString());
	_lblPlaneType->setString(data->getType()->getCString());

	_lblGold->setString(MathUtil::IntToString(playerData->getGold()).c_str());
	_lblUserLevel->setString(MathUtil::IntToString(playerData->getUserExp()).c_str());
	_lblPlaneId->setString(playerData->getPlaneId()->getCString());
	_lblPlaneLevel->setString(MathUtil::IntToString(playerData->getPlaneLevel()).c_str());
	_lblPetType->setString(playerData->getPetType()->getCString());
	_lblPetWeapon->setString(MathUtil::IntToString(playerData->getPetWeapon()).c_str());
}

void MainScene::callbackFBLoginClick( CCObject* sender )
{
#if ((CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS))
	if (EziSocialObject::sharedObject()->isFacebookSessionActive())
	{
		//this->loadSceneWithUserAsLoggedIn();
	}
	else
	{
		EziSocialObject::sharedObject()->performLoginUsingFacebook(false); // Pass true if you need publish permission also
	}
#else
	
#endif
}

void MainScene::fbSessionCallback( int responseCode, const char* responseMessage )
{
	
}

void MainScene::callbackPlaneIdUp( CCObject* sender )
{
	GlobalData::sharedData()->getPlayerData()->setPlaneId(PlaneBank::sharedPlaneBank()->getIdByIndex(++_planeIdIndex));
	updatePlaneInfo();
}

void MainScene::callbackPlaneIdDown( CCObject* sender )
{
	GlobalData::sharedData()->getPlayerData()->setPlaneId(PlaneBank::sharedPlaneBank()->getIdByIndex(--_planeIdIndex));
	updatePlaneInfo();
}

void MainScene::callbackPetIdUp( CCObject* sender )
{
	GlobalData::sharedData()->getPlayerData()->setPetType(PetBank::sharedPetBank()->getPetIdByIndex(++_petIdIndex));
	updatePlaneInfo();
}

void MainScene::callbackPetIdDown( CCObject* sender )
{
	GlobalData::sharedData()->getPlayerData()->setPetType(PetBank::sharedPetBank()->getPetIdByIndex(--_petIdIndex));
	updatePlaneInfo();
}
